<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>$Title$</title>
    <link rel="stylesheet" href="/webjars/element-ui/2.15.13/lib/theme-chalk/index.css">
    <script src="/webjars/vue/2.6.14/dist/vue.js"></script>
    <script src="/webjars/element-ui/2.15.13/lib/index.js"></script>
    <script src="/webjars/axios/1.6.2/dist/axios.js"></script>
</head>
<style>
    body {
        background-color: white;
    }
</style>
<body>
<div id="pro">
    <div>
        <el-form inline>
            <span class="demonstration">开工日期:</span>
            <el-date-picker v-model="dataD"
                            @change="dataTime"
                            type="daterange"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            value-format="yyyy-MM-dd">

            </el-date-picker>
            <el-form-item label="是否完成:">
                <template>
                    <el-radio-group v-model="vo.stateId">
                        <el-radio :label="-1">全部</el-radio>
                        <el-radio :label="1">备货中</el-radio>
                        <el-radio :label="4">生产中</el-radio>
                        <el-radio :label="6">待审核</el-radio>
                        <el-radio :label="10">已完成</el-radio>
                    </el-radio-group>
                </template>
            </el-form-item>
            <el-button type="primary" @click="search">查询</el-button>
        </el-form>
    </div>
    <el-container>
        <template>
            <el-table :data="proList.data" style="width: 100%" height="420">
                <el-table-column prop="proId" label="编号"></el-table-column>
                <el-table-column prop="processNumber" label="加工单号"></el-table-column>
                <el-table-column prop="orderDate" label="单据日期"></el-table-column>
                <el-table-column prop="manuId" label="生产计划"></el-table-column>
                <el-table-column prop="bomId" label="加工工序"></el-table-column>
                <el-table-column prop="goodsId" label="货品编码"></el-table-column>
                <el-table-column prop="goodsName" label="货品名称"></el-table-column>
                <el-table-column prop="spec" label="规格"></el-table-column>
                <el-table-column prop="units" label="计量单位"></el-table-column>
                <el-table-column prop="proNumber" label="生产数量"></el-table-column>
                <el-table-column prop="succedNumber" label="完工数量"></el-table-column>
                <el-table-column label="领料情况">
                    <template slot-scope="scope">
                        <el-button plain :disabled="lingliaoDisabled(scope.row.orderName)" @click="lget(scope.row)">
                            领料
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column label="审核">
                    <template slot-scope="scope">
                        <el-button plain :disabled="isButtonDisabled(scope.row.orderName)" @click="laudit(scope.row)">审核
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="orderName" label="是否完成">
                </el-table-column>
                <el-table-column prop="opendate" label="开工日期"></el-table-column>
                <el-table-column prop="finishdate" label="完工日期"></el-table-column>
                <el-table-column prop="proplant" label="生产车间"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button :disabled="isButton(scope.row.orderName)" @click="prowancheng(scope.row)">生产完成
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </template>
    </el-container>
</div>
<script>
    new Vue({
        el: "#pro",
        data: {
            proList: [],
            dataD: '',
            vo: {
                opendate: '',
                comeTime: '',
                stateId: -1
            },
            //查询回馈值bom
            bomvalue: [],
            resultArray: [],
            entdata: [],
            caidata: {},
            hang: {
                stateId: ''
            },
            stat: '',
            pron: '',
            succ: '',
            stateId: '',
            firstField: '',
            statelist: {},
            checkdata: {},
            ljadddata: {},
            ldatalist: {}
        },
        methods: {
            search() {
                axios.get("/pro/searchpro", {params: this.vo})
                    .then(({data}) => {
                        this.proList = data;
                    })
                this.vo.opendate = ''
                this.vo.comeTime = ''
            },
            dataTime(value) {
                if (value === null) {
                    // 可以选择设置默认值或者清除原有的值
                    this.vo.opendate = null
                    this.vo.comeTime = null
                } else if (Array.isArray(value) && value.length >= 2) {
                    const [opendate, comeTime] = value
                    this.vo.opendate = opendate
                    this.vo.comeTime = comeTime
                } else {
                    console.error("Invalid value format for dataTime:", value)
                }
            },
            //领料
            lget(row) {
                //领料
                this.statelist.proId = row.proId;
                this.statelist.stateId = 4;
                this.statelist.manuId = row.manuId;
                this.statelist.planstate = 4;
                //bomID
                let bomid = row.bomId;
                //总数量
                let numcount = row.proNumber;
                axios.get("/pro/lbomsearch?level=" + bomid)
                    .then(({data}) => {
                        this.bomvalue = data.data;
                        this.compute(numcount);
                    })
            },
            //计算
            compute(numcount) {
                //计算出每个零件所需数量
                this.bomvalue.forEach(p1 => {
                    let result = p1.bomNumber * numcount;
                    let name = p1.bomName;
                    this.resultArray.push([result, name]);
                })
                //循环发送请求
                let count = 0;
                // 创建一个数组来存储所有的Promise
                const promises = this.resultArray.map(key => {
                    return axios.get("/pro/searchnum?name=" + encodeURIComponent(key[1])) // 对URL参数进行编码
                        .then(({data}) => {
                            if (key[0] > data.data) {
                                //采购
                                this.caidata.name = key[1];
                                this.caidata.value = key[0];
                                count++;
                                //添加到采购链接表
                                axios.post("/pur/caiinsert", this.caidata)
                                // 添加采购申请
                            } else {
                                // 根据名称和数量取料
                            }
                            // 返回每个请求的结果，这样Promise.all就知道什么时候所有请求都完成了
                            return data;
                        })
                });

// 使用Promise.all等待所有请求完成
                Promise.all(promises)
                    .then(() => {
                        if (count == 0) {
                            //领取
                            this.resultArray.forEach(key => {
                                const updateData = {
                                    // 假设key[1]是某个唯一标识，比如ID
                                    name: key[1],
                                    // 假设key[0]是要更新的值
                                    value: key[0]
                                };
                                axios.put("/pro/ltpedit", updateData)
                                    .then(({data}) => {
                                        this.$message({
                                            message: key[1] + data.msg,
                                            type: data.code == 200 ? 'success' : 'error'
                                        });
                                        this.ordedit();
                                        this.search();
                                        //修改状态
                                        axios.put("/pro/lsearchintname", this.statelist);
                                    })
                            })
                        } else {
                            //采购
                            this.lopen();
                        }
                    })
            },
            //采购
            lopen() {
                this.$confirm('库存产料不足,是否进入采购程序?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$message({
                        type: 'success',
                        message: '正在进入采购操作...',
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '操作已取消...'
                    });
                });
            },
            //修改订单状态
            ordedit() {
                axios.put("/pro/ordedit", this.statelist);
            },
            //审核
            laudit(row) {
                this.checkdata = row;
                this.statelist.proId = row.proId;
                this.statelist.stateId = 10;
                this.ljadddata = row;
                axios.put("/pro/lsearchintname", this.statelist)
                    .then(({data}) => {
                        //审核通过添加到生产仓库列表中
                        axios.post("/pro/insertdata", this.checkdata)
                            .then(({data}) => {
                                this.$message({
                                    message: data.msg,
                                    type: data.code == 200 ? 'success' : 'error'
                                });
                                this.search();
                            })
                    })

                //获取当前行数据
                //审核通过修改订单状态为通过
                this.statelist.manuId = row.manuId;
                this.statelist.planstate = 10;
                this.ordedit();
                // //如果是半成品要添加到零件仓库
                this.bcpadd(this.ljadddata.spec);
            },
            // 半成品添加
            bcpadd(spec) {
                if (spec == "半成品") {
                    console.log("265")
                    let counts = 0;
                    console.log(this.ljadddata.goodsName)
                    axios.get("/pur/selectname?name=" + this.ljadddata.goodsName)
                        .then(({data}) => {
                            counts = data.data;
                            this.ldatalist.chaseName = this.ljadddata.goodsName;
                            this.ldatalist.chasePrice = 0;
                            this.ldatalist.chaseNumber = this.ljadddata.succedNumber;
                            if (counts == 0) {
                                this.ruku();
                            } else {
                                axios.put("/pur/caiedit", this.ldatalist)
                                    .then(({data}) => {
                                        this.$message({
                                            message: data.msg,
                                            type: data.code == 200 ? 'success' : 'error'
                                        });
                                        this.search();
                                    })
                            }
                        })
                }
            },
            //零件入库
            ruku() {
                console.log(this.ldatalist)
                axios.post("/pur/add", this.ldatalist)
                    .then(({data}) => {
                        this.$message({
                            message: data.msg,
                            type: data.code == 200 ? 'success' : 'error'
                        });
                        this.search();
                    })
            },
            //完成数量操作
            numupdate() {
                axios.put("/pro/numedit", this.statelist)
                    .then(({data}) => {
                        this.search();
                    })
            },
            //生产完成操作
            prowancheng(row) {
                this.statelist.proId = row.proId;
                this.statelist.stateId = 6;
                this.statelist.manuId = row.manuId;
                this.statelist.succednumber = row.proNumber;
                axios.put("/pro/lsearchintname", this.statelist)
                    .then(({data}) => {
                        this.$message({
                            message: data.msg,
                            type: data.code == 200 ? 'success' : 'error'
                        });
                        this.search();
                    })
                this.numupdate();
                this.statelist.manuId = row.manuId;
                this.statelist.planstate = 6;
                this.ordedit();
            },
            isButtonDisabled(orderName) {
                // 假设当 orderName 不是 "通过" 时，按钮应该被禁用
                return orderName !== '待审核';
            },
            isButton(orderName) {
                return orderName !== '生产中'
            },
            lingliaoDisabled(orderName) {
                return orderName !== '备货中'
            }
        },
        mounted() {
            this.search();
        }
    })
</script>
</body>
</html>